package com.example.demo.web.util;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.example.demo.web.Entity.UserEntity;
import com.example.demo.web.Entity.UserLogEntity;
import com.example.demo.web.services.OptAuthService;
import com.fasterxml.jackson.databind.node.ObjectNode;


@Component
public class XinLongUtil {
	private Logger log = LogManager.getLogger(XinLongUtil.class);
	
	@Autowired
    private OptAuthService optauthService;
	@Autowired
    private RabbitTemplate rabbitTemplate;
	
	private String licensedate = "";	
	private int licensedays = 0;
	private int debugflag = 0;
	public int getDebugflag() {
		return debugflag;
	}

	public void setDebugflag(int debugflag) {
		log.info("---------------->>>调试开关变更:" + debugflag);
		this.debugflag = debugflag;
	}

	public static XinLongUtil xinLongUtil;
    
    @PostConstruct
    public void init() {    
    	xinLongUtil = this;
    } 
    
    public void rabbitsend(String queuename,com.fasterxml.jackson.databind.JsonNode message) {
    	rabbitTemplate.convertAndSend(queuename, message);
    }
    
    public int getLicensedays() {
		return licensedays;
	}
    
	public void setLicensedays(int licensedays) {
		this.licensedays = licensedays;
	}

	public String getLicensedate() {
		return licensedate;
	}

	public void setLicensedate(String licensedate) {
		this.licensedate = licensedate;
	}
	
	public void printlog(Logger log,String desc){
		if(this.debugflag != 0){
			log.info(desc);
		}		
	}

    public static String getRemoteAddr(HttpServletRequest request) {
    	String remoteAddr = "";
        if (request != null) {
            remoteAddr = request.getHeader("X-FORWARDED-FOR");
            if (remoteAddr == null || "".equals(remoteAddr)) {
                remoteAddr = request.getRemoteAddr();
            }
        }else{
        	return "";
        }
        return remoteAddr;
    }
    
    public static ObjectNode OptAuth(String userid,String jsonstr,UserEntity sentity){
    	return xinLongUtil.optauthService.optGloablCheck(userid,jsonstr,sentity);
    }
    
    public static int getLoginTryNo(){
    	return xinLongUtil.optauthService.getTryFailNum();
    }
    
    public static int isNeedCheck(String rname){
    	return xinLongUtil.optauthService.isNeedCheck(rname);
    }
    
    public static int isNeedChangePsw(){
    	return xinLongUtil.optauthService.isNeedChangePsw();
    }
    
    public static String getSysTitle(){
    	return xinLongUtil.optauthService.getSysTitle();
    }
    
    public static int saveUserLog(UserLogEntity ulog){
    	return xinLongUtil.optauthService.insertUserLog(ulog);
    }
    
    public static int checkRight(String userid, String rname){
    	return xinLongUtil.optauthService.hasOptAuth(userid, rname);
    }
}
